///********************************************************************
///* Copyright © 2012 fishr (fishr.flash@gmail.com)  
///********************************************************************


package su.fishr.social_network.OK.uploadV2.requests 
{
	
	import flash.events.Event;
	import flash.events.SecurityErrorEvent;
	import flash.events.IOErrorEvent;
	import flash.events.IEventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
   

	/** 
	 * @private 
     * Непосредственно реализует запросы
	 * по указанным адресам. Все остальные
	 * классы задействованные в реализации
	 * конкретных заросов в пакете requests
	 * расширяют данный класс.
	 * 
	 * @playerversion          Flash 10
	 * @langversion            3.0
	 * @author                 fishr
	 * @created                30.05.2012 2:18
	 * @since                  30.05.2012 2:18
	 */
	internal class Requester extends URLLoader
	{
	/**-------------------------------------------------------------------------------
	* 
	*	   						V A R I A B L E ' S 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
		static private const AUTHOR:String = "fishr (fishr.flash@gmail.com)";
		private var _callback:Function;
		private var _errorCall:Function;
	//}
	
	/**-------------------------------------------------------------------------------
	* 
	*	 						P R O P E R T I E S 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
	
	//}
	/**-------------------------------------------------------------------------------
	* 
	*								P U B L I C 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
		/**
		 * Конструктор.
		 * 
		 * @param	callback
		 * @param	errorCall
		 * @param	request
		 */
		public function Requester(callback:Function, errorCall:Function, request:URLRequest = null)
		{
			
			
			init( callback, errorCall );
			super(request);
		}
		
	//}
	
	/**-------------------------------------------------------------------------------
	* 
	*								P R I V A T E 	
	* 
	* --------------------------------------------------------------------------------
	*/	
	//{
	
		private function init( callback:Function, errorCall:Function ):void
		{
			_callback = callback;
			_errorCall = errorCall;
			configureListeners( this );

		}
		
		private function configureListeners(dispatcher:IEventDispatcher):void {
			
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }
		
		private function ioErrorHandler(e:Event):void 
		{
			////////////////////////// T R A C E ///////////////////////////////
			var d:Date = new Date();
			trace(d.valueOf() + ". Requester::ioErrorHandler()  : " + e);
			//////////////////////// E N D  T R A C E //////////////////////////
			throw new Error( e );
			
			_errorCall( UploaderPhotoV2.ANOTHER_ERROR );
		}
		
		private function securityErrorHandler(e:Event):void 
		{
			////////////////////////// T R A C E ///////////////////////////////
			var d:Date = new Date();
			trace(d.valueOf() + ". Requester::securityErrorHandler()  : " + e);
			//////////////////////// E N D  T R A C E //////////////////////////
			throw new Error( e );
			
			_errorCall( UploaderPhotoV2.ANOTHER_ERROR );
		}
		
		private function completeHandler(e:Event):void 
		{
			_callback( this.data );
			
			_callback = null;
		}
	//}
		
		
	}

}